﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OreSystem.BLL;
using OreSystem.Entities;
namespace MainFrom
{
    public partial class CarFrom : Form
    {
        private CarBLL carBLL = new CarBLL();
        private LevelBll levelBll = new LevelBll();
        CartEdit caredit = null;            
        public CarFrom()
        {
            InitializeComponent();
        }

        private void CarFrom_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.Columns.Add("ID", "序号");
            dataGridView1.Columns.Add("PID", "负责人");
            dataGridView1.Columns.Add("CarNo", "汽车编号");
            dataGridView1.Columns.Add("Type", "类型");
            dataGridView1.Columns.Add("LicensePlate", "车牌号");
            dataGridView1.Columns.Add("Cubic", "方/车");
            dataGridView1.Columns.Add("CreateDate", "添加日期");
            dataGridView1.Columns[0].DataPropertyName = "ID";
            dataGridView1.Columns[1].DataPropertyName = "PID";
            dataGridView1.Columns[2].DataPropertyName = "CarNo";
            dataGridView1.Columns[3].DataPropertyName = "Type";
            dataGridView1.Columns[4].DataPropertyName = "LicensePlate";
            dataGridView1.Columns[5].DataPropertyName = "Cubic";
            dataGridView1.Columns[6].DataPropertyName = "CreateDate";
            dataGridView1.DataSource = carBLL.GetCarByType(1);
            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            
            dataGridView1.MultiSelect = false;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            caredit = new CartEdit();
            caredit.Plevels = levelBll.GetLevelByLevel(2);
            caredit.NewCar = null;
            if (caredit.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                dataGridView1.DataSource = carBLL.GetCarByType(1);
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {

            if (dataGridView1.SelectedRows.Count <=0)
            {
                MessageBox.Show("没有选择要修改的数据");
                return;
            }
            caredit = new CartEdit();
            caredit.Plevels = levelBll.GetLevelByLevel(2);
            int id = (int)dataGridView1.CurrentRow.Cells[0].Value;
            caredit.NewCar = carBLL.GetCarById(id);
            if (caredit.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                dataGridView1.DataSource = carBLL.GetCarByType(1);
            }

        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null) {
                MessageBox.Show("没有选择要删除的信息");
                return;
            }
            try {
                if (MessageBox.Show("确定删除吗？", "删除提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK) {
                    int id = (int)dataGridView1.CurrentRow.Cells[0].Value;
                    carBLL.DeleteCar(carBLL.GetCarById(id));
                    dataGridView1.DataSource = carBLL.GetCarByType(1);
                }
            }
            catch (Exception ex) {

                MessageBox.Show("出现错误请联系管理员。错误信息" + ex.Message);
            }
        }

        private void btn_search_Click(object sender, EventArgs e) {
            dataGridView1.DataSource = carBLL.GetListByKey(txt_key.Text,1);
        }
    }
}
